/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.rocketmq.store.index;

import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;
import org.apache.rocketmq.store.MappedFile;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.List;

/**
 * index 索引文件 由indexHead + hash槽 + index条目 组成
 * hash槽:一个indexFile包含500w个hash槽，每个hash槽存储的是落在该hash槽的hashcode的最新的Index的索引
 */
public class IndexFile {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);
    /**
     * 每个hash槽占4字节
     */
    private static int hashSlotSize = 4;
    /**
     * 每个index条目占40字节 hashcode 4字节 + phyoffset 8字节 + timedif 4字节 + pre index no 4字节
     * 具体表现在 mappedByteBuffer 中
     */
    private static int indexSize = 20;
    private static int invalidIndex = 0;
    /**
     * hash槽数目 500w个
     */
    private final int hashSlotNum;
    /**
     * index条目数 2000w个
     */
    private final int indexNum;
    /**
     * 对应一个index
     */
    private final MappedFile mappedFile;
    private final FileChannel fileChannel;
    /**
     * 每条索引的映射缓冲区
     */
    private final MappedByteBuffer mappedByteBuffer;
    /**
     * 索引头 40字节
     */
    private final IndexHeader indexHeader;

    public IndexFile(final String fileName, final int hashSlotNum, final int indexNum, final long endPhyOffset, final long endTimestamp) throws IOException {
        int fileTotalSize = IndexHeader.INDEX_HEADER_SIZE + (hashSlotNum * hashSlotSize) + (indexNum * indexSize);
        this.mappedFile = new MappedFile(fileName, fileTotalSize);
        this.fileChannel = this.mappedFile.getFileChannel();
        this.mappedByteBuffer = this.mappedFile.getMappedByteBuffer();
        this.hashSlotNum = hashSlotNum;
        this.indexNum = indexNum;

        ByteBuffer byteBuffer = this.mappedByteBuffer.slice();
        this.indexHeader = new IndexHeader(byteBuffer);

        if (endPhyOffset > 0) {
            this.indexHeader.setBeginPhyOffset(endPhyOffset);
            this.indexHeader.setEndPhyOffset(endPhyOffset);
        }

        if (endTimestamp > 0) {
            this.indexHeader.setBeginTimestamp(endTimestamp);
            this.indexHeader.setEndTimestamp(endTimestamp);
        }
    }

    public String getFileName() {
        return this.mappedFile.getFileName();
    }

    public void load() {
        this.indexHeader.load();
    }

    public void flush() {
        long beginTime = System.currentTimeMillis();
        if (this.mappedFile.hold()) {
            this.indexHeader.updateByteBuffer();
            this.mappedByteBuffer.force();
            this.mappedFile.release();
            log.info("flush index file elapsed time(ms) " + (System.currentTimeMillis() - beginTime));
        }
    }

    public boolean isWriteFull() {
        return this.indexHeader.getIndexCount() >= this.indexNum;
    }

    public boolean destroy(final long intervalForcibly) {
        return this.mappedFile.destroy(intervalForcibly);
    }

    /**
     * index 条目列表添加条目
     * hashcode:key的hashCode
     * phyoffset:消息对应的物理偏移量
     * timedif：该消息存储时间与第一条消息的时间戳的差值，小于0则无效
     * preIndexNo:该条目的前一条记录的index索引，当出现hash冲突时，构建的链表结构
     *
     * @param key            key
     * @param phyOffset      物理偏移量
     * @param storeTimestamp 存储时间戳
     * @return 是否
     */
    public boolean putKey(final String key, final long phyOffset, final long storeTimestamp) {

        if (this.indexHeader.getIndexCount() < this.indexNum) {
            //根据key算出hashCode
            int keyHash = indexKeyHashMethod(key);
            //对hash槽数量取余，定位到hashcode对应的hash槽下标
            int slotPos = keyHash % this.hashSlotNum;
            //hash槽物理地址 = 头(40字节) + 下标 * 4字节
            int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * hashSlotSize;
            //不同key的hashcode可能相同，不同的key的hashcode对槽数量取余也可能相同，可能引发hash冲突
            FileLock fileLock = null;

            try {

                // fileLock = this.fileChannel.lock(absSlotPos, hashSlotSize,
                // false);

                //读取hash槽中存储的数据，如果hash槽存储的数据小于0或大于当前索引文件中的索引条目格式，则将slotValue设置为0
                int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
                if (slotValue <= invalidIndex || slotValue > this.indexHeader.getIndexCount()) {
                    slotValue = invalidIndex;
                }
                //计算待存储消息的时间戳与第一条消息时间戳的差值，并转换为秒
                long timeDiff = storeTimestamp - this.indexHeader.getBeginTimestamp();

                timeDiff = timeDiff / 1000;

                if (this.indexHeader.getBeginTimestamp() <= 0) {
                    timeDiff = 0;
                } else if (timeDiff > Integer.MAX_VALUE) {
                    timeDiff = Integer.MAX_VALUE;
                } else if (timeDiff < 0) {
                    timeDiff = 0;
                }
                //将条目信息存储在IndexFile中
                //计算新添加条目的起始物理偏移量，等于头部字节长度+hash槽数量*单个hash槽代销(4个字节)+当前Index条目个数*单个Index条目大小(20个字节)
                int absIndexPos = IndexHeader.INDEX_HEADER_SIZE + this.hashSlotNum * hashSlotSize + this.indexHeader.getIndexCount() * indexSize;
                //一次将hashcode，消息物理偏移量，消息存储时间戳与索引文件时间戳，当前hash槽的值存入MappedByteBuffer中
                this.mappedByteBuffer.putInt(absIndexPos, keyHash);
                this.mappedByteBuffer.putLong(absIndexPos + 4, phyOffset);
                this.mappedByteBuffer.putInt(absIndexPos + 4 + 8, (int) timeDiff);
                this.mappedByteBuffer.putInt(absIndexPos + 4 + 8 + 4, slotValue);
                //将当前Index中包含的条目数量存入hash槽中，将覆盖原先hash槽的值。
                this.mappedByteBuffer.putInt(absSlotPos, this.indexHeader.getIndexCount());
                /* 这里是hash冲突链式解决方案的关键实现，hash槽中存储的是该hashcode所对应的最新的index条目的下标，
                 * 新的index条目的最后4个字节存储该hashcode上一个条目的index下标。如果hash槽中存储的值为0或大于当前
                 * IndexFile最大条目数或小于-1，表示该hash槽当前并没有与之对应的index条目。
                 * 值得关注的是，indexFile条目中存储的不是消息索引key而是消息属性key的hashcode，在根据key查找时需要
                 * 根据消息物理偏移量找到消息进而验证消息key的值，之所以只存储hashcode而不存储具体的key，是为了将index条目
                 * 设计为定长结构，才能方便地检索与定位条目。
                 * */
                if (this.indexHeader.getIndexCount() <= 1) {
                    this.indexHeader.setBeginPhyOffset(phyOffset);
                    this.indexHeader.setBeginTimestamp(storeTimestamp);
                }

                this.indexHeader.incHashSlotCount();
                this.indexHeader.incIndexCount();
                this.indexHeader.setEndPhyOffset(phyOffset);
                this.indexHeader.setEndTimestamp(storeTimestamp);
                //更细文件索引头信息。如果当前文件只包含一个条目，更细beginPhyOffset与beginTimestamp，
                //更新endPhyOffset,endTimeStamp,当前文件使用索引条目等信息
                return true;
            } catch (Exception e) {
                log.error("putKey exception, Key: " + key + " KeyHashCode: " + key.hashCode(), e);
            } finally {
                if (fileLock != null) {
                    try {
                        fileLock.release();
                    } catch (IOException e) {
                        log.error("Failed to release the lock", e);
                    }
                }
            }
        } else {
            log.warn("Over index file capacity: index count = " + this.indexHeader.getIndexCount() + "; index max num = " + this.indexNum);
        }
        //如果当前已使用条目大于等于允许最大条目数时，返回false，表示当前索引文件已写满
        return false;
    }

    public int indexKeyHashMethod(final String key) {
        int keyHash = key.hashCode();
        int keyHashPositive = Math.abs(keyHash);
        if (keyHashPositive < 0) keyHashPositive = 0;
        return keyHashPositive;
    }

    public long getBeginTimestamp() {
        return this.indexHeader.getBeginTimestamp();
    }

    public long getEndTimestamp() {
        return this.indexHeader.getEndTimestamp();
    }

    public long getEndPhyOffset() {
        return this.indexHeader.getEndPhyOffset();
    }

    public boolean isTimeMatched(final long begin, final long end) {
        boolean result = begin < this.indexHeader.getBeginTimestamp() && end > this.indexHeader.getEndTimestamp();
        result = result || (begin >= this.indexHeader.getBeginTimestamp() && begin <= this.indexHeader.getEndTimestamp());
        result = result || (end >= this.indexHeader.getBeginTimestamp() && end <= this.indexHeader.getEndTimestamp());
        return result;
    }

    /**
     * 根据索引key查找消息
     *
     * @param phyOffsets 查找到的消息物理偏移量
     * @param key        索引key
     * @param maxNum     本次查找的最大消息条数
     * @param begin      开始时间戳
     * @param end        结束时间戳
     * @param lock       锁
     */
    public void selectPhyOffset(final List<Long> phyOffsets, final String key, final int maxNum, final long begin, final long end, boolean lock) {
        if (this.mappedFile.hold()) {
            //根据key算出key的hashcode
            int keyHash = indexKeyHashMethod(key);
            //然后keyhash对hash槽数量取余定位到hashcode对应的hash槽下标（第几个hash槽）
            int slotPos = keyHash % this.hashSlotNum;
            //hashcode对应的hash槽的物理地址为IndexHeader头部(40字节)加上下标乘以每个hash槽的大小(4字节)
            int absSlotPos = IndexHeader.INDEX_HEADER_SIZE + slotPos * hashSlotSize;

            FileLock fileLock = null;
            try {
                if (lock) {
                    // fileLock = this.fileChannel.lock(absSlotPos,
                    // hashSlotSize, true);
                }

                int slotValue = this.mappedByteBuffer.getInt(absSlotPos);
                // if (fileLock != null) {
                // fileLock.release();
                // fileLock = null;
                // }
                //如果对应的hash槽中存储的数据小于1或大于当前索引条目个数则表示该hashcode没有对应的条目，直接返回
                if (slotValue <= invalidIndex || slotValue > this.indexHeader.getIndexCount() || this.indexHeader.getIndexCount() <= 1) {
                } else {
                    //由于会存在hash冲突，根据slotValue定位该hash槽最新的一个Item条目，将存储的物理偏移加入到phyOffsets中，然后继续验证Item条目中存储的上一个
                    //index下标，如果大于等于1并且小于最大条目数，则继续查找，否则结束查找
                    for (int nextIndexToRead = slotValue; ; ) {
                        if (phyOffsets.size() >= maxNum) {
                            break;
                        }

                        int absIndexPos = IndexHeader.INDEX_HEADER_SIZE + this.hashSlotNum * hashSlotSize + nextIndexToRead * indexSize;

                        int keyHashRead = this.mappedByteBuffer.getInt(absIndexPos);
                        long phyOffsetRead = this.mappedByteBuffer.getLong(absIndexPos + 4);

                        long timeDiff = (long) this.mappedByteBuffer.getInt(absIndexPos + 4 + 8);
                        int prevIndexRead = this.mappedByteBuffer.getInt(absIndexPos + 4 + 8 + 4);
                        //根据index下标定位到条目的起始物理偏移量，然后依次读取hashcode，物理偏移量，时间差，上一个条目的index下标

                        //如果存储的时间差小于0，则直接结束
                        if (timeDiff < 0) {
                            break;
                        }

                        timeDiff *= 1000L;

                        long timeRead = this.indexHeader.getBeginTimestamp() + timeDiff;
                        boolean timeMatched = (timeRead >= begin) && (timeRead <= end);
                        //如果hashcode匹配并且消息存储时间介于待查找时间start，end之间则将消息物理偏移量加入到phyOffsets
                        if (keyHash == keyHashRead && timeMatched) {
                            phyOffsets.add(phyOffsetRead);
                        }
                        //验证条目的前一个index索引，如果索引大于等于1并且小于index条目数，则继续查找，否则结束整个查找；
                        if (prevIndexRead <= invalidIndex || prevIndexRead > this.indexHeader.getIndexCount() || prevIndexRead == nextIndexToRead || timeRead < begin) {
                            break;
                        }

                        nextIndexToRead = prevIndexRead;
                    }
                }
            } catch (Exception e) {
                log.error("selectPhyOffset exception ", e);
            } finally {
                if (fileLock != null) {
                    try {
                        fileLock.release();
                    } catch (IOException e) {
                        log.error("Failed to release the lock", e);
                    }
                }

                this.mappedFile.release();
            }
        }
    }
}
